package com.oilsoft.scm.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.StringTokenizer;

import com.oilsoft.scm.domain.contour.Contour;
import com.oilsoft.scm.domain.contour.Point;
import com.oilsoft.scm.domain.contour.Polygon;
import com.oilsoft.scm.domain.contour.Polyline;

public class ContourFactory {

	public static Contour createContour(BufferedReader in) throws IOException {
		Contour contour = new Contour();
		String line = null;
		line = in.readLine();
		while (!line.contains("Closing plot")) {
			if (line.contains("numrows")) {
				String numRows = (String) line.subSequence(
						line.lastIndexOf("numrows = ") + "numrows = ".length(),
						line.indexOf(","));
				String numCols = (String) line.subSequence(
						line.lastIndexOf("numcols = ") + "numcols = ".length(),
						line.length());
				contour.setNumRows(new Integer(numRows.trim()));
				contour.setNumColumns(new Integer(numCols.trim()));
			} else if (line.contains("Read")) {
				String zValue = (String) line.subSequence(
						line.lastIndexOf("Read") + "Read".length(),
						line.indexOf("z"));
				contour.setZ(new Integer(zValue.trim()));
			} else if (line.contains("Polygon")) {
				String cantPoints = (String) line.subSequence(
						line.lastIndexOf("num=") + "num=".length(),
						line.length());
				Polygon poly = analyzePolygon(in, new Integer(cantPoints.trim()));
				contour.addFigure(poly);
			} else if (line.contains("Polyline")) {
				String cantPoints = (String) line.subSequence(
						line.lastIndexOf("num=") + "num=".length(),
						line.length());
				Polyline poly = analyzePolyline(in, new Integer(cantPoints.trim()));
				contour.addFigure(poly);
			}
			line = in.readLine();
		}
		return contour;
	}

	private static Polygon analyzePolygon(BufferedReader in, Integer points)
			throws IOException {
		Polygon polygon = new Polygon();
		polygon.setNum(points);
		String line = in.readLine();
		String color = (String) line.subSequence(line.lastIndexOf("Index  :")
				+ "Index  :".length(), line.length());
		polygon.setColor(new Integer(color.trim()).intValue());

		for (int i = 0; i < points; i++) {
			line = in.readLine();
			Point point = new Point();
			StringTokenizer st = new StringTokenizer(line, ",");
			point.setIndex(new Integer(st.nextToken().trim()).intValue());
			point.setX(new Double(st.nextToken().trim()).doubleValue());
			point.setY(new Double(st.nextToken().trim()).doubleValue());
			polygon.addPoint(point);
		}
		return polygon;
	}

	private static Polyline analyzePolyline(BufferedReader in, Integer points)
			throws IOException {
		Polyline polyline = new Polyline();
		polyline.setNum(points);
		String line = in.readLine();
		String color = (String) line.subSequence(line.lastIndexOf("Index  :")
				+ "Index  :".length(), line.length());
		polyline.setColor(new Integer(color.trim()).intValue());
		for (int i = 0; i < points; i++) {
			line = in.readLine();
			Point point = new Point();
			StringTokenizer st = new StringTokenizer(line, ",");
			point.setIndex(new Integer(st.nextToken().trim()).intValue());
			point.setX(new Double(st.nextToken().trim()).doubleValue());
			point.setY(new Double(st.nextToken().trim()).doubleValue());
			polyline.addPoint(point);
		}
		return polyline;
	}
}
